<?php

/**
 * Description of ConfigController
 *
 * @author HaoLV
 */
class ConfigController extends ControllerBase {

    public $defaultAction = 'update';

    public function init() {
        parent::init();
        ShortUtil::app()->theme = ADMIN_THEME;
    }

    public function actionCreate($name, $type = null) {
        $model = $this->loadModel($name);

        if ($model !== null)
            $this->redirect(array('update', 'name' => $name));

        $model = new Config;
        $model->name = $name;
        $model->type = $type === null ? 'text' : 'serialize';

        if (isset($_POST['Config'])) {
            if ($model->type == 'text') {
                // Insert configuration type text
                $model->attributes = $_POST['Config'];
                if ($model->save()) {
                    $this->setFlash('Params has been created');
                    $this->redirect(array('update', 'name' => $name));
                }
            } else {
                // Insert configuration type serialize
                $keys = $_POST['Keys'];
                $values = $_POST['Values'];
                $data = array();
                if (is_array($keys)) {
                    foreach ($keys as $k => $v) {
                        if ($v != '')
                            $data[$v] = $values[$k];
                    }
                }
                $model->value = serialize($data);
                if ($model->save()) {
                    $this->setFlash('Params has been created');
                    $this->redirect(array('update'));
                }
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($name=null) {
        if ($name === null)
            $model = Config::model()->findAll('type like "serialize"');
        else
            $model = $this->loadModel($name);

        if ($model === null)
            $this->redirect(array('create', 'name' => $name));

        if (isset($_POST['Config'])) {
            if ($model->type == 'text') {
                $model->attributes = $_POST['Config'];
                if ($model->save()) {
                    $this->setFlash('Params has been created');
                    $this->refresh();
                }
            } else {
                if ($model instanceof Config) {
                    $keys = $_POST['Keys'];
                    $values = $_POST['Values'];
                    $data = array();
                    if (is_array($keys)) {
                        foreach ($keys as $k => $v) {
                            if ($v != '')
                                $data[$v] = $values[$k];
                        }
                    }
                    $model->value = serialize($data);
                    $model->save();
                } else {
                    foreach ($model as $node) {
                        $node->value = serialize($_POST[$node->name]);
                        $node->save();
                    }
                }
                $this->setFlash('Parameters has been updated');
                $this->refresh();
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionClear() {
        Yii::app()->cache->flush();
        $this->actionWriteSubMenu();
        $this->setFlash('Empty cache');
        $this->redirect($_SERVER['HTTP_REFERER']);
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($name) {
        $model = Config::model()->findByPk($name);
        return $model;
    }

    public function actionWriteSubMenu() {
        $html .= "sub2_menu = new Array();\n";

        Yii::import('application.modules.product.models.*');
        $tree = Menu::model()->findAll('type_id=1');
        foreach ($tree as $value) :
            $value->url = str_replace('/', '', $value->alias);
            $term = Term::model()->find('alias =:alias', array(':alias' => $value->url));
            if ($term === null)
                continue;
            $criteria = new CDbCriteria(array('condition' => "tid={$term->id} AND status>0", 'order' => 'name'));

            $count = Brand::model()->count($criteria);
            $brand = Brand::model()->findAll($criteria);

            if (!$count > 1)
                continue;

            $limit = ConfigUtil::get('menu', 'subItem');
            $i = 0;
            $html .= "sub2_menu[{$value->id}] = '";
            $html .= "<div><ul class=\"first\">";
            foreach ($brand as $node) :
                if ($i == $limit) {
                    $html .= "</ul></div>";
                    $html .= "<div><ul>";
                    $i = 0;
                }
                $html .= CHtml::tag('li', array(), CHtml::link(str_replace("'", '', ucwords($node->name)), array('//site/p_sub_cate', 'cate' => $node->cate->alias, 'subCate' => $node->alias)));
                $i++;
            endforeach;
            $html .= "</ul></div>';\n";
        endforeach;

        $file = Yii::getPathOfAlias('root') . "upload/sub_menu.txt";
//        $fh = fopen($file, 'w') or die("Kg the mo file");
//        fwrite($fh, $html);
//        fclose($fh);
    }

}

?>
